package cn.source.zk.service.impl;

import cn.source.common.core.domain.entity.Customer;
import cn.source.zk.mapper.ZkCustomerMapper;
import cn.source.zk.service.IZkCustomerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class ZkCustomerServiceImpl implements IZkCustomerService {

    @Autowired
    private ZkCustomerMapper customerMapper;

    @Autowired
    private ZkAreaServiceImpl areaService;

    /**
     * 客户分页列表
     * @param customer
     * @return
     */
    public List<Customer> pageList(Customer customer) {
        //区分是客户页面筛选还是其他页面调用
        if (customer.getIsEnabled() == null){
            customer.setIsEnabled(1);
        }

        List<Customer> customers = customerMapper.selectPageList(customer);

        for (Customer item : customers) {
            if (!Objects.isNull(item.getArea())){
                item.setAncestorNames(areaService.getAncestorName(item.getArea()));
            }
        }

        return customers;
    }

    @Override
    public List<Customer> pageListALL(Customer customer) {
        //区分是客户页面筛选还是其他页面调用
        List<Customer> customers = customerMapper.selectPageList(customer);

        for (Customer item : customers) {
            if (!Objects.isNull(item.getArea())){
                item.setAncestorNames(areaService.getAncestorName(item.getArea()));
            }
        }

        return customers;
    }

    /**
     * 检查客户是否存在
     * @param phone
     * @return
     */
    public boolean checkCustomerExist(String phone,Long num) {
        Customer customer = new Customer();
        customer.setPhone(phone);
        customer.setNum(num);
        int result = customerMapper.selectPageList(customer).size();
        return result > 0;
    }

    /**
     * 新增客户
     * @param customer
     * @return
     */
    public int save(Customer customer) {
        return customerMapper.insert(customer);
    }

    /**
     * 根据num查询客户
     * @param num
     * @return
     */
    public Customer getByNum(Long num) {
        return customerMapper.getByNum(num);
    }

    /**
     * 修改客户
     * @param customer
     * @return
     */
    public int editCustomer(Customer customer) {
        return customerMapper.update(customer);
    }

    /**
     * 删除客户
     * @param nums
     * @return
     */
    public int del( List<Long> nums) {
        return customerMapper.del(nums);
    }
}
